package trastienda.rest;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import trastienda.modelo.Producto;

/**
 * Servlet implementation class JSONServlet
 */
@WebServlet("/JSONServlet")
public class JSONServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public JSONServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		Producto p = new Producto();
		p.setIdProducto(1);
		p.setNombre("Lego");
		p.setDescripcion("Juego de Lego");
		
		Producto p2 = new Producto();
		p2.setIdProducto(2);
		p2.setNombre("Playgo");
		p2.setDescripcion("Juego de Playgo");
		
		JSONArray arrayObj = new JSONArray();
		arrayObj.add(p);
		arrayObj.add(p2);
		
		PrintWriter out = response.getWriter();
		out.println(arrayObj);
//		for (int i = 0; i < arrayObj.size(); i++) {
//			out.println(arrayObj.getString(i));
//		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
